package com.wildwolf.project.wildwolf.ui.buddy;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.location.Location;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.mapapi.BMapManager;
import com.baidu.mapapi.GeoPoint;
import com.baidu.mapapi.ItemizedOverlay;
import com.baidu.mapapi.LocationListener;
import com.baidu.mapapi.MapActivity;
import com.baidu.mapapi.MapController;
import com.baidu.mapapi.MapView;
import com.baidu.mapapi.MyLocationOverlay;
import com.baidu.mapapi.OverlayItem;
import com.wildwolf.project.wildwolf.R;
import com.wildwolf.project.wildwolf.config.AppConfig;
import com.wildwolf.project.wildwolf.location.WildwolfLocationManager;
/**
 * 
 * 
 * @author LEON
 * 
 */
public class ForgatherActivity extends MapActivity implements LocationListener,OnClickListener {
	/**
	 * 
	 */
	private BMapManager bMapManager = null;
	/**
	 * 
	 */
	private TextView textView_return = null;
	/**
	 * 
	 */
	private TextView textView_filter = null;
	/**
	 * 
	 */
	private List<Map<String, Double>> overlayCoordinates;
private AvatarItemizedOverlayHandler avatarItemizedOverlayHandler = null;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		//设置全屏  
        /*getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,   
                WindowManager.LayoutParams.FLAG_FULLSCREEN);  */
		setContentView(R.layout.forgather_layout);
		initMapView();
		/**
		 * 
		 */
		textView_filter = (TextView) findViewById(R.id.forgather_textView_filter);
		textView_return = (TextView) findViewById(R.id.forgather_textView_return);
		/**
		 *初始化监听器
		 */
		textView_filter.setOnClickListener(this);
		textView_return.setOnClickListener(this);
	}

	/**
	 * 初始化地图
	 */
	private void initMapView() {
		bMapManager = new BMapManager(getApplication());
		bMapManager.init(AppConfig.MAP.BAIDU_API_KEY, null);
		super.initMapActivity(bMapManager);
		Location location = WildwolfLocationManager.getInstence(this).getLocation();
		MapView mapView = (MapView) findViewById(R.id.forgatherMapsView);
		/**
		 * 
		 */
		// mapView.setTraffic(true);//实时交通状况
		mapView.setSatellite(true);
		mapView.setBuiltInZoomControls(true);
		MapController mapController = mapView.getController(); //获得地图控制器
		GeoPoint point = new GeoPoint((int) (location.getLatitude() * 1E6),
				(int) (location.getLongitude() * 1E6)); 
		mapController.setCenter(point); 
		mapController.setZoom(12); // 设置缩放级别
		mapController.animateTo(point);
		
		/**
		 * 当前位置
		 * 
		 */
		MyLocationOverlay myLocationOverlay = new  MyLocationOverlay(this,mapView);
		myLocationOverlay.enableMyLocation();
		myLocationOverlay.enableCompass();
		mapView.getOverlays().add(myLocationOverlay);
		/**
		 * 
		 */
	mapView.getOverlays().add(new  AvatarItemizedOverlay(getResources().getDrawable(R.drawable.mark),this,new GeoPoint((int)(location.getLatitude()*1E6),(int)( location.getLongitude()*1E6))));
	}

	@Override
	protected void onStart() {
		super.onStart();
		avatarItemizedOverlayHandler = new  AvatarItemizedOverlayHandler();
		/**
		 * 
		 */
	}

	@Override
	protected boolean isRouteDisplayed() {
		return false;
	}

	@Override
	protected void onDestroy() {
		if (bMapManager != null) {
			bMapManager.destroy();
			bMapManager = null;
		}
		super.onDestroy();
	}

	@Override
	protected void onPause() {
		if (bMapManager != null) {
			bMapManager.stop();
		}
		super.onPause();
	}

	@Override
	protected void onResume() {
		if (bMapManager != null) {
			bMapManager.start();
		}
		super.onResume();
	}

	@Override
	public void onClick(View v) {
		if (v == textView_return) {
			finish();
		}else{
			Intent intent = new Intent(this, ConditionActivity.class);
			startActivity(intent);
		}
	}
	/**
	 * 更新自己当前位置的Handler
	 * @author LEON
	 *
	 */
	class AvatarItemizedOverlayHandler extends Handler{
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			System.out.println(msg.getData().getDouble("latitude")+msg.getData().getDouble("longitude"));
		}
	}
	class AvatarItemizedOverlay extends ItemizedOverlay<OverlayItem>{
		/**
		 * 
		 */
		private Context context;

		/**
		 * 所有在线头像图层的集合
		 */
		 private List<OverlayItem> overlayItems = new ArrayList<OverlayItem>();

		public AvatarItemizedOverlay(Drawable drawable, Context context,GeoPoint geoPoint) {
			this(drawable, context);
			overlayItems.add(new  OverlayItem(geoPoint, String.valueOf(geoPoint.getLatitudeE6()),String.valueOf(geoPoint.getLongitudeE6())));
			 populate();
		}
		public AvatarItemizedOverlay(Drawable drawable, Context context) {
			super(boundCenter(drawable));
			this.context= context;
		}
		public List<Map<String, Double>> getOverlayCoordinates() {
			return overlayCoordinates;
		}
		@Override
		protected OverlayItem createItem(int idnex) {
			return overlayItems.get(idnex);
		}

		@Override
		public int size() {
			return overlayItems.size();
		}

		@Override
		protected boolean onTap(int index) {
			  Toast.makeText(this.context, overlayItems.get(index).getSnippet(),
		                Toast.LENGTH_SHORT).show();
		        return true;
		}
		
	}
	@Override
	public void onLocationChanged(Location location) {
	Message msg = new  Message();
	Bundle bundle = new  Bundle();
	bundle.putDouble("latitude", location.getLatitude());
	bundle.putDouble("longitude", location.getLongitude());
	msg.setData(msg.getData());
	ForgatherActivity.this.avatarItemizedOverlayHandler.sendMessage(msg);
	}
}
